package com.lzy.cloud.order.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.cloud.order.dtos.OrderInfoCarSaleDto;
import com.lzy.cloud.order.dtos.rvcar.CarSaleOrderDto;
import com.lzy.cloud.order.facade.OrderInfoFacade;
import com.lzy.cloud.order.remote.BaseApi;
import com.lzy.cloud.order.request.OrderInfoCarSaleRequest;
import com.lzy.cloud.order.response.MemberToken;
import com.lzy.cloud.order.response.OrderInfoCarSaleResponse;
import com.lzy.cloud.order.service.OrderInfoCarSaleService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @description:房车销售订单子表
 * @author: liyun
 * @create: 2018-07-05 13:49
 */
@RestController
@RequestMapping("/order/car/sale")
public class OrderInfoCarSaleController{
    @Autowired
    private OrderInfoCarSaleService orderInfoCarSaleService;
    @Autowired
    private OrderInfoFacade orderInfoFacade;
    @Autowired
    private BaseApi baseApi;

    /**
     * 根据订单id查询房车销售订单子表
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/find/{orderId}",method = RequestMethod.GET)
    public Result queryByOrderId(@PathVariable("orderId") String orderId) {
        Ensure.that(orderId).isNotNull("00010003");
        OrderInfoCarSaleDto orderInfoCarSaleDto = orderInfoCarSaleService.queryByOrderId(orderId);
        return new Result(DozerUtils.map(orderInfoCarSaleDto, OrderInfoCarSaleResponse.class));
    }

    /**
     * 新增房车销售订单子表
     * @param request
     * @return
     */
    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public Result create(@RequestHeader("token") String token, OrderInfoCarSaleRequest request) {
        Ensure.that(request).isNotNull("00000002");
        Ensure.that(token).isNotNull("00000002");
        MemberToken memberToken = DozerUtils.map(baseApi.isMemberLogin(token).getResult(),MemberToken.class);
        Ensure.that(memberToken).isNotNull("00001033");
        request.setCreateUser(memberToken.getId());
        request.setMemberId(memberToken.getId());
        return new Result(orderInfoFacade.createOrder(DozerUtils.map(request, CarSaleOrderDto.class)));
    }

    /**
     * 更新房车销售订单子表
     * @param request
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result update(@RequestBody OrderInfoCarSaleRequest request) {
        Ensure.that(request).isNotNull("00000002");
        boolean result = orderInfoCarSaleService.update(DozerUtils.map(request, OrderInfoCarSaleDto.class));
        Ensure.that(result).isTrue("00070003");
        return new Result(result);
    }

    /**
     * 查询房车销售订单(分页)
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result selectList(OrderInfoCarSaleRequest request) {
        Ensure.that(request).isNotNull("00010001");
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        PageInfo<OrderInfoCarSaleDto> pageInfo = orderInfoCarSaleService.selectList(pager, DozerUtils.map(request, OrderInfoCarSaleDto.class));
        if (pageInfo != null && pageInfo.getList() != null) {
            pager.setTotal(pageInfo.getTotal());
            return new Result(pager, DozerUtils.maps(pageInfo.getList(), OrderInfoCarSaleResponse.class));
        } else {
            return new Result();
        }
    }
}
